package com.ces.newPOS.admin.dao.server;

import com.ces.newPOS.admin.dao.client.UserDao;
import com.ces.newPOS.admin.dao.server.util.QuerySqlUtil;
import com.ces.newPOS.admin.entity.User;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJBException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Dao
 */
@Stateless
public class UserDaoImpl implements UserDao
{
  @PersistenceContext(name="newPOS")
  protected EntityManager em;

    @Override
  public User findByPrimaryKey(Integer pk) throws ObjectNotFoundException
  {
    User user = this.em.find(User.class, pk);
    if (user == null)
    {
      throw new ObjectNotFoundException("User not found with primary key '" + pk + "'!");
    }

    return user;
  }

    @Override
  public Collection<User> findByGroupId(Integer groupId)
  {
    String sql = "select o from User o where o.groupId = :groupId and o.disabled = false";
    Query query = this.em.createQuery(sql);
    query.setParameter("groupId", groupId);

    return query.getResultList();
  }

    @Override
  public Collection<Integer> getAllPrimaryKeys()
  {
    String sql = "select o.id from User o where o.disabled = false order by o.id";
    Query query = this.em.createQuery(sql);

    return query.getResultList();
  }

    @Override
  public Collection<User> getAllUsers()
  {
    String sql = "select o from User o where o.disabled = false order by o.id";
    Query query = this.em.createQuery(sql);

    return query.getResultList();
  }

    @Override
  public User createUser(User user)
  {
    user.prePersist();
    this.em.persist(user);

    return user;
  }

    @Override
    public User addUser(String name, String password) {
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        user.prePersist();
        this.em.persist(user);

        return user;
    }
  

    @Override
  public User disableUser(Integer userId) throws ObjectNotFoundException
  {
    User user = this.findByPrimaryKey(userId);
    if (!user.isDisabled())
    {
      user.setDisabled(true);
      user.preUpdate();

      user = this.em.merge(user);
    }

    return user;
  }

    @Override
  public User updateUser(User user)
  {
    user.preUpdate();

    return this.em.merge(user);
  }

    @Override
  public List findByUsername(String username)
  {
    String sql = "select o from User o where o.name = :username and o.disabled = false";
    Query query = this.em.createQuery(sql);
    query.setParameter("username", username);
    return query.getResultList();
  }

    @Override
  public User findByLoginId(String loginId) throws ObjectNotFoundException
  {
    String sql = "select o from User o where o.loginId = :loginId and o.disabled = false";
    Query query = this.em.createQuery(sql);
    query.setParameter("loginId", loginId);

    List<User> userList = query.getResultList();
    if (userList.isEmpty())
    {
      throw new ObjectNotFoundException("User with login ID = '" + loginId + "' cann't be found!");
    }
    else if (userList.size() > 1)
    {
      throw new EJBException("There is more than one login ID '" + loginId + "' in User table!");
    }

    return userList.get(0);
  }

    @Override
  public Collection<Integer> getAllPrimaryKeysWithDisabled()
  {
    String sql = "select o.id from User o order by o.id";
    Query query = this.em.createQuery(sql);

    return query.getResultList();
  }

    @Override
  public Collection<Integer> findPrimaryKeysByKeyword(String keyword)
  {
    String sql = "select o.id from User o where (o.name like :username or o.loginId like :loginId) and o.disabled = false order by o.id";

    Query query = this.em.createQuery(sql);

    QuerySqlUtil.setParams(query, sql, ":username", QuerySqlUtil.getAllLikeString(keyword));
    QuerySqlUtil.setParams(query, sql, ":loginId", QuerySqlUtil.getAllLikeString(keyword));

    return query.getResultList();
  }

    @Override
  public Collection<Integer> findPrimaryKeysByLoginIdAndUsername(String loginId, String username)
  {
    String sql = "select o.id from User o where (o.name like :username and o.loginId like :loginId) and o.disabled = false order by o.id";

    Query query = this.em.createQuery(sql);

    QuerySqlUtil.setParams(query, sql, ":username", QuerySqlUtil.getAllLikeString(username));
    QuerySqlUtil.setParams(query, sql, ":loginId", QuerySqlUtil.getAllLikeString(loginId));

    return query.getResultList();
  }

  @Override
  public Map findByUsernameandPassword(String username, String password)
  {
    String sql = "select o from User o where o.name = :username and o.password=:password and o.disabled = false";

    Query query = this.em.createQuery(sql);

    query.setParameter("username", username);
    query.setParameter("password", password);
    
    List<User> userList = query.getResultList();
    Map userinfo = new HashMap();
    if (! userList.isEmpty()){
        userinfo.put("userId",userList.get(0).getId());
        userinfo.put("username",userList.get(0).getName());
        userinfo.put("userpassword",userList.get(0).getPassword());
    }
    return userinfo;
  }
}
